const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { SRC_PATH, DIST_PATH } = require('./utils');

const webpackConfig = {
  target: 'node',
  entry: {
    server: path.join(SRC_PATH, 'app.js')
  },
  output: {
    path: DIST_PATH,
    filename: '[name].bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new webpack.DefinePlugin({ // 创建全局常量
      'process.env': {
        NODE_ENV: process.env.NODE_ENV === 'production' ? 'production' : 'development'
      }
    })
  ],
  externals: [nodeExternals()],
  node: {
    console: true,
    global: true,
    process: true,
    __filename: true,
    __dirname: true,
    Buffer: true,
    setImmediate: true
  }
};

module.exports = webpackConfig;